# -*- coding:utf-8 -*-
# @Time: 2020/8/24 17:43
# @Author: liupengfei
# @File: serializers.py
from django.conf import settings
from rest_framework import serializers
from utils.serializersfield import ImageUploadCharField, BooleanCheckCharField
from .models import House, Room


class HouseSerializer(serializers.ModelSerializer):
    style_text = serializers.CharField(source='style.name', read_only=True)
    thumbnail_path = serializers.SerializerMethodField()
    thumbnail_arr = ImageUploadCharField(source='thumbnail', read_only=True)
    is_complete_check = BooleanCheckCharField(source='is_complete', read_only=True)
    rooms = serializers.SerializerMethodField()

    def get_rooms(self, obj):
        return RoomSerializer(obj.house_rooms.filter(delete=False), many=True).data

    def get_thumbnail_path(self, obj):
        if obj.thumbnail:
            return obj.thumbnail.get_url()
        return ''

    class Meta:
        model = House
        fields = '__all__'


class RoomSerializer(serializers.ModelSerializer):
    thumbnail_path = serializers.SerializerMethodField()
    thumbnail_arr = ImageUploadCharField(source='thumbnail', read_only=True)

    def get_thumbnail_path(self, obj):
        if obj.thumbnail:
            return obj.thumbnail.get_url()
        return ''

    class Meta:
        model = Room
        fields = '__all__'
